local WnUtil = require("utils/wnutil")
local UpvalueUtil = require("utils/upvalueutil")

local brain = require("brains/witch_gisela_minibrain")

local function fn()
    local inst = Prefabs["eyeofterror_mini"].fn()

    inst:AddTag("witch_gisela_mini")
    inst:AddTag("ignore_armor")

    if not TheWorld.ismastersim then
        return inst
    end

    inst:SetBrain(brain)

    return inst
end

local function grounded_fn()
    local inst = Prefabs["eyeofterror_mini_grounded"].fn()

    if not TheWorld.ismastersim then
        return inst
    end

    if inst._hatch_task ~= nil then
        inst._hatch_task:Cancel()
    end

    local _try_to_hatch = UpvalueUtil.GetUpvalue(Prefabs["eyeofterror_mini_grounded"].fn, "try_to_hatch")
    local function try_to_hatch(inst)
        return WnUtil.HookSpawnPrefab("eyeofterror_mini", "witch_gisela_mini", _try_to_hatch, inst)
    end

    inst._hatch_task = inst:DoTaskInTime(TUNING.EYEOFTERROR_MINI_EGGTIME, try_to_hatch)

    return inst
end

local function projectile_fn()
    local inst = Prefabs["eyeofterror_mini_projectile"].fn()

    if not TheWorld.ismastersim then
        return inst
    end

    local _onhitfn = inst.components.complexprojectile.onhitfn
    inst.components.complexprojectile.onhitfn = function(inst, ...)
        return WnUtil.HookSpawnPrefab("eyeofterror_mini_grounded", "witch_gisela_mini_grounded", _onhitfn, inst, ...)
    end

    return inst
end

return Prefab("witch_gisela_mini", fn),
    Prefab("witch_gisela_mini_grounded", grounded_fn),
    Prefab("witch_gisela_mini_projectile", projectile_fn)
